package com.dg.util;

import com.dg.pojo.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;

import java.util.Date;

@Data
public class JwtUtil {

    private final static String KEY = "morongqin";

    //24小时
    private final static long TTL = 360000*24;

    public static String createJwt(String id, User user){
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        JwtBuilder builder = Jwts.builder().setId(id).setSubject(user.getUsername()).
                signWith(SignatureAlgorithm.HS256, KEY).setIssuedAt(now)
                .setExpiration(new Date(nowMillis+TTL))
                .claim("username",user.getUsername())
                .claim("phone", user.getPhone())
                .claim("deptId", user.getDeptId())
                .claim("remark",user.getRemark());
        return builder.compact();
    }

    public static Claims parseJwt(String jwt){
        return Jwts.parser().setSigningKey(KEY).parseClaimsJws(jwt).getBody();
    }

}
